﻿using System;
using System.ComponentModel;
using Microsoft.WowAddonStudio.FrameXml.Serialization;

namespace Microsoft.WowAddonStudio.FrameXml.Components.Descriptors
{
    public static class FrameXmlDescriptorFactory
    {
        /// <summary>
        /// Creates the property.
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="instance">The instance.</param>
        /// <param name="propertyDescriptor">The property descriptor.</param>
        /// <param name="propertyName">Name of the property.</param>
        /// <returns></returns>
        public static PropertyDescriptor CreateProperty<T>(T instance, PropertyDescriptor propertyDescriptor, string propertyName) where T : class
        {
            if (typeof(T) == typeof(InheritableObject) || typeof(T).IsSubclassOf(typeof(InheritableObject)))
                return new InheritableObjectPropertyDescriptor(instance as InheritableObject, propertyDescriptor, propertyName);

            return new FrameXmlPropertyDescriptor<T>(instance, propertyDescriptor, propertyName);
        }

        /// <summary>
        /// Creates the property.
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="propertyDescriptor">The property descriptor.</param>
        /// <returns></returns>
        public static PropertyDescriptor CreateProperty<T>(PropertyDescriptor propertyDescriptor) where T : class
        {
            return FrameXmlDescriptorFactory.CreateProperty<T>(null, propertyDescriptor, null);
        }

        /// <summary>
        /// Creates the event.
        /// </summary>
        /// <param name="componentType">Type of the component.</param>
        /// <param name="eventName">Name of the event.</param>
        /// <param name="attributes">The attributes.</param>
        /// <returns></returns>
        public static EventDescriptor CreateEvent(Type componentType, EventName eventName, params Attribute[] attributes)
        {
            if (componentType == null)
                throw new ArgumentNullException("componentType");

            return new FrameXmlEventDescriptor(componentType, eventName, attributes);
        }
    }
}
